package com.service.impl;

import com.dao.ClazzMapper;
import com.model.Clazz;
import com.service.ClazzService;
import com.utils.PageParam;
import com.utils.Pager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("clazzService")
public class ClazzServiceImpl implements ClazzService {

    @Resource
    private ClazzMapper clazzMapper;

    @Override
    public Pager<Clazz> getClazzPage(String year, String clazzName, PageParam pageParam) {
        Pager<Clazz> pager = new Pager<>(0, new ArrayList<>());
        String search = "";
        if (StringUtils.isNotEmpty(clazzName)) {
            search = "%" + clazzName + "%";
        }
        int totalCount = clazzMapper.findClazzCount(year, search);
        if (totalCount > 0) {
            List<Clazz> list = clazzMapper.findClazzs(year, search, pageParam);
            pager.setTotalCount(totalCount);
            pager.setList(list);
        }
        return pager;
    }

    @Override
    public void updateClazz(Clazz clazz) {
        clazzMapper.updateClazz(clazz);
    }

    @Override
    public void saveClazz(Clazz clazz) {
        clazzMapper.saveClazz(clazz);
    }

    @Override
    public void delClazz(List<Long> ids) {
        clazzMapper.delClazz(ids);
    }

    @Override
    public List<Clazz> getAllClazz() {
        return clazzMapper.findAllClazz();
    }

    @Override
    public List<String> getTeacherClazzCodes(Long id) {
        return clazzMapper.findTeacherClazzCodes(id);
    }
}
